package com.springmvc;

import java.io.IOException;
import java.io.InputStream;
import java.util.Collection;
import java.util.Date;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller
public class EmployeeHandler {
	@Autowired
	private EmployeeDao employeeDao;
	
	@RequestMapping("/testJson")
	@ResponseBody
	public Collection<Employee> getAll(){
		return employeeDao.getAll();
	}
	@ResponseBody
	@RequestMapping(value="/testHttpMessageConvert")
	public String testHttpMessageConvert(@RequestBody  String  requestBody){
		System.out.println("请求内容:");
		System.out.println(requestBody);
		return "Hello "+new Date();
	}
	@RequestMapping("/testResponseEntity")
	public ResponseEntity<byte[]> testResponesEntity(HttpSession session) throws Exception{
		ServletContext servletContext = session.getServletContext();
		InputStream inputStream = servletContext.getResourceAsStream("/files/abc.txt");
		byte[] body = new byte[inputStream.available()];
		inputStream.read(body);
		HttpHeaders headers=new HttpHeaders();
		headers.add("Content-Disposition", "attachment;filename=abc.txt");
		ResponseEntity<byte[]> entity=new ResponseEntity<byte[]>(body, headers,HttpStatus.OK);
		return entity;
	}
	
	
}
